/*
 * @Description:
 * @Author: qingyang
 * @Date: 2022-01-10 16:36:57
 * @LastEditors: qingyang
 * @LastEditTime: 2022-03-07 15:25:31
 */
import messages from './message';
import { createI18n } from 'vue-i18n';
import { createApp } from 'vue';

// Gets the current locale
export function getLanguage() {
  // Use the language of choice
  const chooseLang = localStorage.getItem('locale');
  if (chooseLang) return chooseLang;
  // if not choose language
  const lang = (navigator.language || (navigator as any).browserLanguage).toLowerCase();
  const locales = Object.keys(messages);
  return locales.includes(lang) ? lang : 'zh';
}

export const i18n = createI18n({
  locale: getLanguage(),
  messages
});
export default (app: ReturnType<typeof createApp>) => {
  app.use(i18n);
};
